// Load the SDK Asynchronously
(function (d) {
    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement('script');
    js.id = id;
    js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    ref.parentNode.insertBefore(js, ref);
}(document));

window.fbAsyncInit = function () {

    FB.Event.subscribe('auth.statusChange', function (response) {
        $(document).trigger('fbStatusChange', response);
    });

    FB.init({
        appId: '408169062666556',
        status: true, // check login status
        cookie: true, // enable cookies to allow the server to access the session
        xfbml: true, // parse XFBML
        scope: permissions
    });

    /* All the events registered */
    FB.Event.subscribe('auth.login', function (response) {
        // do something with response
        login();
    });
    FB.Event.subscribe('auth.logout', function (response) {
        // do something with response
        logout();
    });

    FB.getLoginStatus(function (response) {
        if (response.session) {
            // logged in and connected user, someone you know
            login();
        }
    });
    
    FB.api('/me', function(response) {
    }); 
};

function graphStreamPublish() {//바로 포스팅
    var body = document.URL;
    if (document.getElementById('Msg') != null)
        body = document.getElementById('Msg').value;

    FB.api('/me/feed', 'post', {message: body}, function (response) {
        if (!response || response.error) {
            alert('Error occured');
        } else {
            alert('Post ID: ' + response.id);
        }
    });
};

function setStatus() {
    status1 = document.getElementById('Msg').value;
    FB.api(
            {
                method: 'status.set',
                status: status1
            },
    function (response) {
        if (response == 0) {
            alert('Your facebook status not updated. Give Status Update Permission.');
        }
        else {
            alert('Your facebook status updated');
        }
    }
    );
}

function share() {
    var share = {
        method: 'stream.share',
        u: document.URL
    };

    FB.ui(share, function (response) {
        console.log(response);
    });
}

// Specify the extended permissions needed to view user data
// The user will be asked to grant these permissions to the app (so only pick those that are needed)
        var permissions = [
          'email',
          'user_likes',
          'friends_likes',
          'user_about_me',
          'friends_about_me',
          'user_birthday',
          'friends_birthday',
          'user_education_history',
          'friends_education_history',
          'user_hometown',
          'friends_hometown',
          'user_relationships',
          'friends_relationships',
          'user_relationship_details',
          'friends_relationship_details',
          'user_location',
          'friends_location',
          'user_religion_politics',
          'friends_religion_politics',
          'user_website',
          'friends_website',
          'user_work_history',
          'friends_work_history'
          ].join(',');

// Specify the user fields to query the OpenGraph for.
// Some values are dependent on the user granting certain permissions
        var fields = [
          'id',
          'name',
          'first_name',
          'middle_name',
          'last_name',
          'gender',
          'locale',
          'languages',
          'link',
          'third_party_id',
          'installed',
          'timezone',
          'updated_time',
          'verified',
          'age_range',
          'bio',
          'birthday',
          'cover',
          'currency',
          'devices',
          'education',
          'email',
          'hometown',
          'interested_in',
          'location',
          'political',
          'payment_pricepoints',
          'favorite_athletes',
          'favorite_teams',
          'picture',
          'quotes',
          'relationship_status',
          'religion',
          'significant_other',
          'video_upload_limits',
          'website',
          'work'
          ].join(',');

  function showDetails() {
    FB.api('/me', {fields: fields}, function(details) {
      // output the response
      var stringifyStr = JSON.stringify(details, null, '\t');
      var detailInfo = JSON.parse(stringifyStr);
      $('#myInfo').html(stringifyStr);
      $('#myInfo').html('<table>'
              +'<tr><th>Profile</th><th>Detail</th></tr>'
              +'<tr><td rowspan="4"><img src="'+detailInfo.picture.data.url+'" alt="Profile pic"></td>'+'<td>Name: '+detailInfo.nam+'</td></tr>'
              +'<tr><td>From: '+detailInfo.locale+'</td></tr>'
              +'<tr><td>age: At Least '+detailInfo.age_range.min+'years old'+'</td></tr>'
              +'<tr><td>His phone:'+detailInfo.devices[0].os+'</td></tr>'
              +'</table>');
      $('#fb-login').attr('style', 'display:none;');
    });
  }